import { Body, Controller, Delete, Get, Param, Post, Put, Query } from '@nestjs/common';
import { CreateRoleDto } from './dto/create-role.dto';
import { FindAllRoleDto } from './dto/find-all-role.dto';
import { RoleService } from './role.service';

@Controller('roles')
export class RoleController {
  constructor(private readonly roleService: RoleService) {}

  @Post()
  async create(@Body() createRoleDto: CreateRoleDto) {
    await this.roleService.create(createRoleDto);
    return '创建成功';
  }

  @Get('page')
  findAll(@Query() query: FindAllRoleDto) {
    return this.roleService.findAll(query);
  }

  @Put(':id')
  async update(@Param('id') id: number, @Body() data: CreateRoleDto) {
    await this.roleService.update(id, data);
    return '编辑成功';
  }

  @Delete(':ids')
  async removeMany(@Param('ids') ids: string) {
    // 将传入的字符串ids（比如"2,3"）转换为数字数组
    const idArray = ids.split(',').map(Number);
    await this.roleService.removeMany(idArray);
    return '删除成功';
  }

  @Get(':id/form')
  findForm(@Param('id') id: number) {
    return this.roleService.findForm(id);
  }

  @Get(':id/menuIds')
  findMenuIds(@Param('id') id: number) {
    return this.roleService.findMenuIds(id);
  }

  @Put(':id/menus')
  async updateMenus(@Param('id') id: number, @Body() data: number[]) {
    await this.roleService.updateMenus(id, data);
    return '修改成功';
  }

  @Get('options')
  findOptions() {
    return this.roleService.findOptions();
  }
}
